package com.tedu.Controller;

import com.tedu.pojo.Item;
import com.tedu.service.ItemService;
import com.tedu.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/11/23
 */
@Slf4j
@RestController
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        log.info("port:{}",port);
        if (Math.random()<0.9){
            long t = new Random().nextInt(1000);
            log.info("延迟：{}",t);
            Thread.sleep(t);
        }
        return JsonResult.ok(itemService.getItems(orderId));
    }

    @PostMapping("/decreaseNumber")
    public void decreaseNum(@RequestBody List<Item> items){

        itemService.decreaseNumbers(items);
    }

}
